package com.tlgen.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rxjava.rxlife.RxLife;
import com.tlgen.R;
import com.tlgen.adapter.PayOrderAdapter;
import com.tlgen.bean.PayOrder;
import com.tlgen.bean.PayOrderResult;
import com.xuexiang.xui.widget.actionbar.TitleBar;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import rxhttp.RxHttp;

public class PayOrderActivity extends AppCompatActivity {

    private RecyclerView recyclerView;//声明RecyclerView
    private PayOrderAdapter repayAdapter;//声明适配器
    private Context context;
    private ArrayList<PayOrder> payOrderArrayList;

    TitleBar mTitleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        context = this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repay);
        recyclerView = (RecyclerView) findViewById(R.id.repay_recyclerview);

        mTitleBar = findViewById(R.id.pay_order_titlebar);
        mTitleBar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        sendGet();
        initData();

        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(repayAdapter);

    }

    private void sendGet() {
        // 通过 getSharedPreferences 获取当前用户的token
        String token = getSharedPreferences("login_info", MODE_PRIVATE).getString("token", "");
        System.out.println("当前用户登录的token: " + token);
        RxHttp.get("/repay/data/list")
                .addHeader("Authorization", token) // 携带token的请求头
                .asClass(PayOrderResult.class)
                .to(RxLife.toMain(this))  //感知生命周期，并在主线程回调
                .subscribe(result -> {
                    ArrayList<PayOrder> arrayList = new ArrayList<>();
                    for (int i = 0; i < result.getRows().size(); i++) {
                        arrayList.add(result.getRows().get(i));
                        // 保存结果
                        SharedPreferences.Editor editor = getSharedPreferences("payOrderArrayList", MODE_PRIVATE).edit();
                        Gson gson = new Gson();
                        String json = gson.toJson(arrayList);
                        editor.putString("arrayList", json);
                        editor.commit();
                    }
                }, (error)  -> {
                    System.out.println(error);
                });
    }

    private void initData() {
        SharedPreferences preferences = getSharedPreferences("payOrderArrayList", MODE_PRIVATE);
        String json = preferences.getString("arrayList", null);
        System.out.println("获取到的json为: " + json);
        if (json != null)
        {
            Gson gson = new Gson();
            Type type = new TypeToken<List<PayOrder>>(){}.getType();
            ArrayList<PayOrder> arrayList = new ArrayList<PayOrder>();
            arrayList = gson.fromJson(json, type);
            payOrderArrayList = arrayList;

            repayAdapter = new PayOrderAdapter(context, payOrderArrayList);

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
